<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">

            {{# if(d.status=='0' ){ }}
            <a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="goPay">前往缴费</a>
            {{# } }}

        </script>

        <script type="text/html" id="statusTemp">
            {{#  if(d.flag == 1){ }}
            <input type="checkbox" name="status" lay-skin="switch" checked   lay-text="开启|关闭" value= {{d.id}}    lay-filter="status" >
            {{#  } else if(d.flag == 0) { }}
            <input type="checkbox" name="status" lay-skin="switch"  lay-text="开启|关闭" value= {{d.id}}  lay-filter="status" >
            {{#  } }}
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            url: 'broadband/queryAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'name', width: 150, title: '套餐名称'},
                {field: 'price', width: 300, title: '价格'},
                {field: 'createTime', width: 100, title: '创建时间'},
                {field: 'overTime', width: 200, title: '过期时间'},
                {field: 'flag', width: 200, title: '是否自动续费', templet:'#statusTemp'},
                {field: 'status', width: 150, title: '处理状态',
                    templet: function(res){
                        if(res.status=='0'){
                            return '<span class="layui-badge layui-bg-red" >未缴费</span>';
                        }else if (res.status == '1'){
                            return '<span class="layui-badge layui-bg-gray" >已缴费</span>';
                        }else if (res.status == '2'){
                            return '<span class="layui-badge layui-bg-gray" >已过期</span>';
                        }
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}

            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            // skin: 'line',
            id: 'testReload'
        });


        var $ = layui.$, active = {
            reload: function(){
                var identity = $('#identity');
                var tel = $('#tel');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        identity:identity.val(),
                        tel:tel.val()
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'goPay') {
                layer.confirm('确定要前往缴费吗？', function (index) {
                    // console.log(data.id)
                    window.location.href = payUrl+"/payment/payBroadband?id=" + data.id
                });


            }

        });

        form.on('switch(status)', function(data){
            var id = data.value;
            var status = this.checked ? '1' : '0';
            console.log(id+"" , status)
            $.ajax({
                type: 'POST',
                url: '/broadband/updateByUser/',
                contentType:'application/json',
                data:JSON.stringify({"id" :id,"flag":status}) ,
                beforeSend:function(){
                    index = layer.msg('正在切换中，请稍候',{icon: 16,time:false,shade:0.8});
                },
                error: function(data){
                    console.log(data);
                    layer.msg('数据异常，操作失败！');
                },
                success: function(data){
                    layer.msg('操作成功！')
                },
            });
        });











    });
</script>